package com.javalong.androidmorning.wechat.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.javalong.androidmorning.dao.ArticleDetailDao;
import com.javalong.androidmorning.pojo.ArticleDetail;

/**
 * 主要对微信的请求及返回数据作处理
 * 
 * @author Administrator
 *
 */
@Service
public class ArticleDetailService {

	@Autowired
	private ArticleDetailDao articleDetailDao;

	public List<ArticleDetail> getList(int page, int pageSize) {
		return articleDetailDao.findAll(page * pageSize, pageSize);
	}

	public void delete(int id) {
		articleDetailDao.delete(id);
	}

	/**
	 * 文章内容中有很多<br></br>，都去掉，再存入
	 * @param article
	 */
	public void save(ArticleDetail article) {
		Date now = new Date();
		article.setCreateAt(now);
		article.setUpdateAt(now);
		String articleContet = article.getContent();
		if(!StringUtils.isEmpty(articleContet)){
			//替换换行符
			article.setContent(articleContet.replaceAll("<br></br>", ""));
		}
		articleDetailDao.save(article);
	}

	public int getCount() {
		return articleDetailDao.getCount();
	}

	public void update(ArticleDetail article) {
		Date now = new Date();
		article.setUpdateAt(now);
		String articleContet = article.getContent();
		if(!StringUtils.isEmpty(articleContet)){
			//替换换行符
			article.setContent(articleContet.replaceAll("<br></br>", ""));
		}
		articleDetailDao.update(article);
	}

	public ArticleDetail findById(int id) {
		return articleDetailDao.findById(id);
	}

}
